<?php
    $top_menu_1 = FALSE;
    $top_menu_2 = FALSE;
    $top_menu_3 = FALSE;
    $top_menu_4 = TRUE;
    $top_menu_5 = FALSE;
    $top_menu_6 = FALSE;
    $top_menu_7 = FALSE;
    include('header.php');
    include('sidebar2.php');
    
    // PAGE LIMIT
    if(isset($_GET['p'])){
	$page = $_GET['p'];
    }else{
	$page = 1;
    }
    
    $sql_relate = 'SELECT * FROM hs_news ORDER BY news_date DESC';
    
    $page_limit = 9;
    $grouplimit = 10;
    $start_row = (($page-1))*$page_limit;
    $sql_relate_nolimit = $sql_relate;
    $sql_relate .= " LIMIT $start_row,$page_limit";
    // PAGE LIMIT END

    // PAGE LIMIT
    $count_row = mysql_query($sql_relate_nolimit);
    $count_row2 = mysql_num_rows($count_row);
    $count_row3 = ceil($count_row2/$page_limit) ;
    
    if($count_row3>1)
    {
	if($count_row3<$grouplimit)
	{
	    for($i = 1 ; $i<=$count_row3 ; $i++) {
		if($i==$page){
		    $limitlink .=  "<span>".$i."</span>&nbsp;";
		}else{
		    $limitlink .= "<a href='?".$catselect."p=$i'>&nbsp;$i&nbsp;</a>&nbsp;";
		}
	    }
	}
	else
	{
	    $pagegroup = ceil($page/$grouplimit); // Find group of current page
	    $allpagegroup = ceil($count_row3/$grouplimit); // Total page group
	    if($pagegroup==1) // If First Group
	    {
		if($page>1)
		{
		    $limitlink .= "<a href='?".$catselect."p=".($page-1)."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;";
		}
		$i=($pagegroup*$grouplimit)-($grouplimit-1);
		$j=($pagegroup*$grouplimit);
		for($i ; $i<=$j ; $i++) {
		    if($i==$page){
			$limitlink .= "<span>".$i."</span>&nbsp;";
		    }else{
			$limitlink .= "<a href='?".$catselect."p=$i'>&nbsp;$i&nbsp;</a>&nbsp;";
		    }
		}
		$limitlink .= "...&nbsp;<a href='?".$catselect."p=".($page+1)."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=$count_row3' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
	    }
	    else if($pagegroup==$allpagegroup) // If Last Group
	    {
		$limitlink = "<a href='?".$catselect."p=1' title='First Pages'>&nbsp;<<&nbsp;first&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=".($page-1)."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;...&nbsp;";
		$i=($pagegroup*$grouplimit)-($grouplimit-1);
		$j=$count_row3;
		for($i ; $i<=$j ; $i++) {
		    if($i==$page){
			$limitlink .= "<span>".$i."</span>&nbsp;";
		    }else{
			$limitlink .= "<a href='?".$catselect."p=$i'>&nbsp;$i&nbsp;</a>&nbsp;";
		    }
		}
		if($page<$count_row3)
		{
		    $limitlink .= "&nbsp;<a href='?".$catselect."p=".($page+1)."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
		    $limitlink .= "<a href='?".$catselect."p=$count_row3' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
		}
	    }
	    else
	    {
		$limitlink = "<a href='?".$catselect."p=1' title='First Pages'>&nbsp;<<&nbsp;first&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=".($page-1)."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;...&nbsp;";
		$i=($pagegroup*$grouplimit)-($grouplimit-1);
		$j=($pagegroup*$grouplimit);
		for($i ; $i<=$j ; $i++) {
		    if($i==$page){
			$limitlink .=  "<span>".$i."</span>&nbsp;";
		    }else{
			$limitlink .= "<a href='?".$catselect."p=$i'>&nbsp;$i&nbsp;</a>&nbsp;";
		    }
		}
		$limitlink .= "...&nbsp;<a href='?".$catselect."p=".($page+1)."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=$count_row3' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
	    }
	}
    }
    
    $rss = $db->query($sql_relate);
    $start = 0;
    while ($line = $db->fetchNextObject($rss)){
        
        $class = '';
        if((($start++)%3)==0){
            $class = 'news_list_first';
        }
        
        $date = date('d.m.Y',$line->news_date);
        
        if(strlen($line->news_title)>30){
            $title = substr_utf8($line->news_title,0,27).'...';
        }else{
            $title = $line->news_title;
        }
        
        if(strlen(strip_tags($line->news_detail))>70){
            $desc = substr_utf8(strip_tags($line->news_detail),0,67).'...';
        }else{
            $desc = $line->news_detail;
        }
	
	if($line->news_img!='' AND file_exists(APPPATH.'/images/news/'.$line->news_id.'/'.$line->news_img)){
	    $newImg = URL.'/images/news/'.$line->news_id.'/'.$line->news_img;
	}else{
	    $newImg = URL.'/images/noimg.jpg';
	}
        
        $newsList .= <<<newsList
            <div class="news_list {$class}">
                <a href="news-event-detail.php?id={$line->news_id}" title="{$line->news_title}"><img src="{$newImg}" alt="{$line->news_title}" /></a>
                <img src="images/news_list_line.jpg" class="news_list_line" />
                <a href="news-event-detail.php?id={$line->news_id}" title="{$line->news_title}"><h3 class="news_title">{$title}</h3></a>
                <p class="news_detail">
                    {$desc}
                </p>
                <p class="news_date">{$date}</p>
                <img src="images/news_list_line.jpg" class="news_list_line" />
            </div>
newsList;
    }
?>
            <div id="main_content" class="news_content">
                <img src="images/news_title.jpg" alt="News & Event" id="news_title" />
                
                <div id="news_list">
                    
                    <?php echo $newsList; ?>
                    
                     <div class="clear">&nbsp;</div>
                     
                     <div id="news_list_ctrl"><?php echo $limitlink; ?></div>
                </div>
                
            </div>
            <div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>
